<template>
  <div style="height: 920px">
    <el-row :gutter="20">
      <!-- 左侧树 -->
      <el-col :span="7" :xs="24">
        <div class="col-tree">
          <div class="title">{{ $t("巡检对象树") }}</div>
          <div class="tree-input">
            <el-input
              v-model="objTreeInput"
              :placeholder="$t('节点对象名称巡检点名称识别类型')"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="tree-body">
            <el-tree
              :data="deptOptions"
              node-key="treeId"
              :props="defaultProps"
              :expand-on-click-node="false"
              :default-checked-keys="[]"
              show-checkbox
              class="scroll-hidden"
              ref="objTree"
              default-expand-all
            >
              <!-- @node-click="nodeClick($event)" -->
              <span
                style="display: inline-block; width: 100%"
                :style="
                  data.treeId == currentTreeId
                    ? 'background-color:rgba(0, 209, 255, 0.2)'
                    : ''
                "
                slot-scope="{ node, data }"
              >
                <img
                  :src="icons[data.iconType]"
                  style="width: 16px; height: 16px"
                />
                <span
                  style="margin-left: 10px"
                  :style="{
                    color:
                      data.type == '2' && data.isParam == '0'
                        ? '#FF5562'
                        : '#FFFFFF',
                  }"
                  :title="data.name"
                  >{{ data.name }}</span
                >
              </span>
            </el-tree>
          </div>
        </div>
      </el-col>
      <!-- 右侧 -->
      <el-col :span="17" :xs="24">
        <div class="right-col">
          <div class="search-area-btn btns">
            <div
              class="serch-btn cursor-pointer table-top"
              @click="analysisClick('℃')"
            >
              {{ $t("温度分析") }}
            </div>
            <div
              class="serch-btn cursor-pointer table-top"
              @click="analysisClick('V')"
            >
              {{ $t("电压分析") }}
            </div>
            <div
              class="serch-btn cursor-pointer table-top"
              @click="analysisClick('A')"
            >
              {{ $t("电流分析") }}
            </div>
            <div
              class="serch-btn cursor-pointer table-top"
              @click="analysisClick('Pa')"
            >
              {{ $t("压力分析") }}
            </div>
          </div>
          <!-- 如果有数据 -->
          <div v-if="showData">
            <!-- 搜索区域 -->
            <div class="search-area">
              <div class="form">
                <el-form
                  :model="listQuery"
                  ref="queryForm"
                  size="mini"
                  :inline="true"
                  label-width="130px"
                >
                  <el-form-item :label="$t('开始时间') + ':'">
                    <el-date-picker
                      v-model="listQuery.startTime"
                      type="datetime"
                      size="medium"
                      :placeholder="$t('选择开始时间')"
                      format="yyyy-MM-dd HH:mm:ss"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      default-time="['00:00:00', '23:59:59']"
                      :picker-options="pickerOptionsStart"
                    >
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item :label="$t('结束时间') + ':'">
                    <el-date-picker
                      v-model="listQuery.endTime"
                      type="datetime"
                      size="medium"
                      :placeholder="$t('选择结束时间')"
                      format="yyyy-MM-dd HH:mm:ss"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      default-time="['00:00:00', '23:59:59']"
                      :picker-options="pickerOptionsEnd"
                    >
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item :label="$t('任务名称') + ':'" prop="taskName">
                    <el-input
                      v-model="listQuery.taskName"
                      :placeholder="$t('请输入')"
                      clearable
                      size="medium"
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                </el-form>
              </div>
              <div class="btns search-area-btn">
                <div
                  v-hasPermi="['data:objectData:search']"
                  @click="searchBtn"
                  class="serch-btn cursor-pointer"
                  style="width: 80px"
                >
                  {{ $t("搜索") }}
                </div>
                <div
                  v-hasPermi="['data:objectData:reset']"
                  @click="resetBtn"
                  class="reset-btn cursor-pointer"
                  style="width: 80px"
                >
                  {{ $t("重置") }}
                </div>
              </div>
            </div>
            <!-- 图表 -->
            <div class="charts">
              <div>
                <div
                  class="line-chart"
                  id="lineChart"
                  :style="{ width: '98%', height: '400px' }"
                ></div>
              </div>
            </div>
            <!-- 表格 -->
            <div class="table">
              <div class="self">
                <el-table
                  ref="tableRef"
                  :data="tableData"
                  tooltip-effect="dark"
                  style="width: 100%"
                >
                  <el-table-column type="selection"> </el-table-column>
                  <el-table-column
                    width="80"
                    prop="taskExecId"
                    :label="$t('任务ID')"
                    align="center"
                  >
                  </el-table-column>
                  <el-table-column
                    width="100"
                    :label="$t('任务名称')"
                    prop="taskName"
                    align="center"
                    show-overflow-tooltip
                  >
                  </el-table-column>
                  <el-table-column
                    width="100"
                    prop="deviceName"
                    :label="$t('巡检对象名称')"
                    align="center"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="createTime"
                    :label="$t('识别时间')"
                    show-overflow-tooltip
                    align="center"
                    width="150"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="recognitionName"
                    :label="$t('识别类型')"
                    show-overflow-tooltip
                    align="center"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="deviceResult"
                    :label="$t('识别结果')"
                    show-overflow-tooltip
                    align="center"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="resource"
                    :label="$t('采集信息')"
                    show-overflow-tooltip
                    align="center"
                  >
                    <template slot-scope="scope">
                      <span
                        v-for="(item, index) in scope.row.resourceType"
                        :key="index"
                        class="column-imgs"
                      >
                        <img
                          src="../../../assets/images/objectData/picture.png"
                          alt=""
                          @click="rowClick(scope.row, index, 'showPic')"
                          v-if="item == '0'"
                        />
                        <img
                          src="../../../assets/images/objectData/video.png"
                          alt=""
                          @click="rowClick(scope.row, index, 'showVideo')"
                          v-if="item == '1'"
                        />
                        <img
                          src="../../../assets/images/objectData/audio.png"
                          alt=""
                          @click="rowClick(scope.row, index, 'showAudio')"
                          v-if="item == '2'"
                        />
                      </span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="checkStatus"
                    :label="$t('审核结果')"
                    show-overflow-tooltip
                    align="center"
                  >
                    <template slot-scope="scope">
                      <div
                        :style="{
                          color: scope.row.checkStatus == 0 ? '' : 'gray',
                        }"
                      >
                        {{
                          scope.row.checkStatus == 0
                            ? $t("未审核")
                            : $t("已审核")
                        }}
                      </div>
                    </template>
                  </el-table-column>
                  <!-- <el-table-column label="操作" width="60" align="center">
                    <template slot-scope="scope">
                      <div
                        @click="auditRowClick(scope.row)"
                        class="serch-btn column-btn"
                      >
                        审核
                      </div>
                    </template>
                  </el-table-column> -->
                </el-table>
              </div>
              <div class="pagenation">
                <pagination
                  :total="total"
                  :page.sync="listQuerySubmit.pageNum"
                  :limit.sync="listQuerySubmit.pageSize"
                  @pagination="getTableList"
                >
                </pagination>
              </div>
            </div>
          </div>
          <div v-else class="placeholder-box">
            <div>
              <img
                src="../../../assets/images/objectData/placeholder.png"
                alt=""
                class="placeholder-img"
              />
            </div>
            <div>
              <el-row>{{ $t("对比分析") }}</el-row>
              <el-row
                >{{ $t("请先在左侧对象树上勾选多个单位相同的对象") }}
              </el-row>
              <el-row>{{
                $t("再点击上方的温度分析电压分析电流分析按钮")
              }}</el-row>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- dialog -->
    <el-dialog
      :title="$t('查看')"
      :visible.sync="dialogVisible"
      width="560px"
      @close="closeDialog"
    >
      <!-- 图片 -->
      <div
        v-if="
          dialogBooArr.find((item) => {
            return item.title == 'showPic';
          }).bool
        "
      >
        <div class="dialog-lineone">
          <div>{{ $t("拍摄时间") + "：" }}{{ dialogDetail.createTime }}</div>
          <div>{{ $t("文件名") + "：" }}{{ dialogDetail.resourceName }}</div>
          <div @click="downLoad" class="el-icon-download cursor-pointer"></div>
        </div>
        <div>
          <img style="width: 100%" :src="dialogSrc" alt="" />
        </div>
      </div>
      <!-- 视频 -->
      <div
        v-if="
          dialogBooArr.find((item) => {
            return item.title == 'showVideo';
          }).bool
        "
      >
        <div class="dialog-lineone">
          <div>{{ $t("录制时间") }}{{ dialogDetail.createTime }}</div>
          <div>{{ $t("文件名") }}{{ dialogDetail.resourceName }}</div>
          <div @click="downLoad" class="el-icon-download cursor-pointer"></div>
        </div>
        <div>
          <video controls style="width: 100%" ref="showVideo">
            <source :src="dialogSrc" type="video/mp4" />
          </video>
        </div>
      </div>
      <!-- 音频 -->
      <div
        v-if="
          dialogBooArr.find((item) => {
            return item.title == 'showAudio';
          }).bool
        "
      >
        <div class="dialog-lineone">
          <div>{{ $t("录制时间") }}{{ dialogDetail.createTime }}</div>
          <div>{{ $t("文件名") }}{{ dialogDetail.resourceName }}</div>
          <div @click="downLoad" class="el-icon-download cursor-pointer"></div>
        </div>
        <div class="display:flex" v-if="currentAudioUrl.src != ''">
          <aplayer :music="currentAudioUrl" ref="showAudio"></aplayer>
          <div class="change-audio">
            <div class="reset-btn el-icon-arrow-left"></div>
            <div class="reset-btn el-icon-arrow-right"></div>
          </div>
        </div>
      </div>
      <div slot="footer" style="display: flex; justify-content: flex-end">
        <div class="reset-btn" @click="dialogVisible = false">
          {{ $t("取消") }}
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import aplayer from "vue-aplayer";
import moment from "moment";
import { API_treeOption, API_resultCompared } from "@/api/data/objData";
import {
  API_devictpageInfoList,
  API_getResourceinfo,
} from "@/api/data/inspectrecords.js";
import * as echarts from "echarts";
/**
 *数据分析
 */
export default {
  dicts: ["recognition", "structType"],
  components: {
    aplayer,
  },
  data() {
    return {
      sevePort: window.location.protocol + "//" + window.location.host,
      // 结束日期
      pickerOptionsStart: {
        disabledDate: (time) => {
          let endDateVal = this.listQuery.endTime;
          if (endDateVal) {
            return (
              time.getTime() >=
              new Date(endDateVal).getTime() + 1 * 24 * 60 * 60 * 1000
            );
          }
        },
      },
      // 结束日期
      pickerOptionsEnd: {
        disabledDate: (time) => {
          let beginDateVal = this.listQuery.startTime;
          if (beginDateVal) {
            return time.getTime() < new Date(beginDateVal).getTime();
          }
        },
      },
      showData: false,
      objTreeInput: undefined, //搜索框数据
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询条件
      listQuery: {
        taskName: "",
        startTime: "",
        endTime: "",
        identifyStatusList: [],
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      listQuerySubmit: {},
      lineType: "", //折线图类型 温度-电压-电流-压力
      tableData: [], //表格数据
      selectedTableData: [], //表格选中数据
      //tree图标
      icons: {
        1: require("@/assets/images/objectData/rootobj.png"), //根节点
        2: require("@/assets/images/objectData/jiedianobj.png"), //节点
        3: require("@/assets/images/objectData/ptstobj.png"), //普通实体对象
        4: require("@/assets/images/objectData/ptxnobj.png"), //普通虚拟对象
        5: require("@/assets/images/objectData/hsstobj.png"), //红色实体对象
        6: require("@/assets/images/objectData/hsxnobj.png"), //红色虚拟对象
      },
      currentTreeId: "", // 被选中的树id 且type==2
      dialogSrc: "", //弹框URL
      dialogDetail: {}, //弹框图片数据
      currentAudioUrl: {
        src: "",
      }, //音频
      dialogVisible: false, //图片dialog
      //控制弹框展示组件
      dialogBooArr: [
        {
          title: "showPic",
          bool: false,
        },
        {
          title: "showVideo",
          bool: false,
        },
        {
          title: "showAudio",
          bool: false,
        },
      ],
      treeIds: [], //被选中的树节点
      downLoadUrl: "",
    };
  },
  watch: {
    objTreeInput(val) {
      this.getTreeData(val);
    },
  },
  created() {
    this.listQuerySubmit = Object.assign({}, this.listQuery);
    this.getTableList(); //表格数据
    this.getTreeData(""); //树节点数据
  },
  mounted() {},
  methods: {
    /**分析按钮 集合 */
    analysisClick(type) {
      //获取对象数据选中节点
      this.treeIds = this.$refs.objTree.getCheckedNodes().filter((item) => {
        return item.type == "2";
      });
      this.treeIds = this.treeIds.map((item) => item.treeId);
      if (this.treeIds.length == 0) {
        return this.$message.info(this.$t("请先选择巡检对象数据"));
      }
      //展示数据
      this.getTableList();
      this.showData = true;
      this.lineType = type;
      //绘制折线图
      this.paintLine("lineChart");
    },
    /**折线图数据 */
    paintLine(id) {
      let params = {};
      params = {
        nodeList: this.treeIds,
        unit: this.lineType,
        startTime: this.listQuerySubmit.startTime,
        endTime: this.listQuerySubmit.endTime,
        taskName: this.listQuerySubmit.taskName,
      };
      API_resultCompared(params).then((res) => {
        if (res.code == 200) {
          // 纵坐标单位
          let unitY = "";
          unitY = res.list[0].unit;
          //图例
          let newArr = [];
          let legend = [];
          newArr = res.list;
          legend = newArr.map((item) => item.recognition);
          let source = res.list;
          var target = [];
          for (var i in source) {
            var data = [];
            var lineList = source[i].lineList;
            for (var j in lineList) {
              data.push([
                moment(lineList[j].xdata).format("YYYY-MM-DD HH:mm:ss"),
                lineList[j].ydata,
              ]);
            }
            var obj = {
              data: data,
              type: "line",
              name: source[i].recognition,
            };
            target.push(obj);
          }
          let percentOfTen = res.list.map((item) => {
            return item.percentOfTen;
          });
          //取最近十天
          let minTool = String(100 - Math.min.apply(null, percentOfTen));
          // 获取Dom
          var chartDom = document.getElementById(id);
          // 初始化实例;
          var lineChart = echarts.init(chartDom);
          if (!chartDom) {
            return;
          }
          // 绘制配置
          var options;
          options = {
            title: {
              textStyle: {
                fontWeight: "normal",
              },
              left: "50%",
              top: "10",
            },
            //线的颜色
            tooltip: {
              trigger: "axis",
            },
            color: ["#FE8800", "#73F6AF"],
            legend: {
              data: legend,
              textStyle: {
                fontSize: 15,
                color: "#fff",
              },
              bottom: "40",
            },
            // 折线图内边距
            grid: {
              left: "3%",
              right: "5%",
              bottom: "25%",
              containLabel: true,
            },
            //用于区域缩放。以下配置只可缩放横轴
            dataZoom: [
              {
                type: "slider",
                bottom: 65, //下滑块距离x轴底部的距离
                show: true,
                start: minTool,
                end: 100,
                xAxisIndex: [0],
              },
            ],
            xAxis: {
              type: "time", // x轴为 时间轴
              name: "t",
              splitLine: { show: false },
              axisLine: {
                lineStyle: { width: 0, color: "#314450" },
              },
              axisLabel: {
                color: "#BFCAD4",
                fontSize: "14",
              },
              axisTick: { show: false },
              boundaryGap: false,
            },
            yAxis: {
              axisLabel: {
                color: "rgba(191, 202, 212, 1)",
                fontSize: 11,
              },
              type: "value",
              name: unitY, //纵坐标单位
              nameTextStyle: {
                color: " #BFCAD4",
                nameLocation: "start",
                fontSize: "14",
              },
              min: 0, //取0为最小刻度
              scale: true, //自适应
              nameGap: 15,
              axisTick: {
                show: false, //刻度线
              },
              axisLine: {
                show: false, //隐藏y轴
              },
              splitLine: {
                // ⽹格线
                show: true,
                lineStyle: {
                  color: "#2e3f48",
                  type: "dotted",
                  width: "2",
                },
              },
            },
            series: target,
          };
          options && lineChart.setOption(options);
          lineChart.on("click", function (params) {
            // 控制台打印数据的名称
            console.log(params, "chartDomchartDomchartDom");
          });
        }
      });
    },
    /**搜索 */
    searchBtn() {
      this.listQuery.pageNum = 1;
      this.listQuerySubmit = Object.assign({}, this.listQuery);
      //折线图数据
      this.analysisClick(this.lineType);
      //表格数据
      this.getTableList();
    },
    /**树 点击 */
    nodeClick(node) {
      this.listQuery.pageNum = 1;
      this.listQuerySubmit = Object.assign({}, this.listQuery);
      this.currentTreeId = node.treeId;
      this.getTableList();
    },
    /**表格点击视频音频事件 */
    rowClick(rowInfo, index, type) {
      let id = rowInfo.resourceId[index];
      //展示图片、视频、音频
      this.dialogSrc = "";
      this.dialogVisible = true;
      API_getResourceinfo({ id: id }).then((res) => {
        if (res.code == 200) {
          if (res.data) {
            switch (type) {
              case "showPic":
                this.dialogSrc = this.sevePort + res.data.resourceUrl;
                break;
              case "showVideo":
                this.$nextTick(() => {
                  this.$refs.showVideo.src =
                    this.sevePort + res.data.resourceUrl;
                });
                break;
              case "showAudio":
                this.currentAudioUrl = {
                  src: this.sevePort + res.data.resourceUrl,
                  title: res.data.resourceName,
                };
                break;
            }
            this.dialogDetail = res.data;
            this.downLoadUrl = res.data.resourceUrl;
          } else {
            this.$message.error(this.$t("暂无数据"));
          }
        } else {
          this.dialogSrc = "";
          this.dialogDetail = {};
        }
      });
      this.dialogBooArr.forEach((item) => {
        if (item.title == type) {
          item.bool = true;
        } else {
          item.bool = false;
        }
      });
    },
    downLoad() {
      this.$download.resource(this.downLoadUrl);
    },
    /**table数据 */
    getTableList() {
      let obj = {
        ...this.listQuerySubmit,
        startTime: this.listQuerySubmit.startTime,
        endTime: this.listQuerySubmit.endTime,
        nodeList: this.treeIds,
        unit: this.lineType,
      };
      API_devictpageInfoList(obj).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          if (res.rows.length > 0) {
            this.tableData.forEach((item) => {
              let resourceType = item.resourceType || "";
              let resourceId = item.resourceId || "";
              item.resourceType = resourceType.split(",");
              item.resourceId = resourceId.split(",");
            });
          }
          this.total = Number(res.total);
          this.listQuery = Object.assign({}, this.listQuerySubmit);
        }
      });
    },
    /** 查询部门下拉树结构 */
    getTreeData(data) {
      API_treeOption({ name: data }).then((res) => {
        if (res.code == "200") {
          this.deptOptions = res.deviceTrees;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    /**table 多选数据 */
    selectionChange(val) {
      this.selectedTableData = val;
    },
    /**close dialog */
    closeDialog() {
      this.dialogVisible = false;
      this.dialogBooArr.forEach((item) => {
        if (item.title == "showPic") return;
        if (item.bool) {
          this.$refs[item.title].pause();
        }
      });
    },
    /**重置 */
    resetBtn() {
      this.listQuery = {
        taskName: "",
        startTime: "",
        endTime: "",
        checkStatusList: [],
        identifyStatusList: [],
        pageNum: 1,
        pageSize: 10,
      };
      this.$refs.objTree.setCheckedKeys([]);
      this.currentTreeId = "";
      this.searchBtn();
    },
  },
};
</script>
<style lang="scss" scoped>
// 树
.col-tree {
  height: 920px;
  padding: 24px;
  box-sizing: border-box;
  border-right: 2px solid #314353;
  .title {
    font-size: 18px;
    font-weight: 500;
    color: #00d1ff;
  }
  .tree-input {
    margin-top: 15px;
  }
}
.right-col {
  height: 920px;
  box-sizing: border-box;
  .btns {
    display: flex;
    justify-content: flex-start;
    padding: 24px 0;
    border-left: 0;
    width: 100%;
    border-bottom: 1px dashed rgba(219, 255, 251, 0.2);
    div {
      margin-right: 16px;
    }
  }
}
//搜索
.search-area {
  display: flex;
  width: 100%;
  box-sizing: border-box;
  // background-color: rgba(255, 255, 255, 0.08);
  .form {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    padding: 22px 16px 0 16px;
  }
  .btns {
    display: flex;
    width: 200px;
    padding: 8px;
    justify-content: space-around;
    align-items: center;
    border-left: 1px dashed rgba(219, 255, 251, 0.2);
  }
}
//表格
.table {
  height: 474px;
  padding: 24px;
  box-sizing: border-box;
  position: relative;
  .pagenation {
    position: absolute;
    bottom: 100px;
    right: 20px;
  }
}
.column-imgs {
  img {
    width: 24px;
    height: 24px;
    cursor: pointer;
  }
}
.column-btn {
  display: flex;
  width: 44px;
  height: 32px;
  justify-content: center;
  align-items: center;
  font-size: 14px;
}
//占位符
.placeholder-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  padding-top: 120px;
  align-content: center;
  align-items: center;
  color: rgba(255, 255, 255, 0.8);
  font-family: SourceHanSansSC-Regular, SourceHanSansSC;
  line-height: 24px;
  font-size: 14px;
  .placeholder-img {
    display: inline-block;
    width: 252px;
    height: 252px;
  }
}
// 折线图
.charts {
  height: 361px;
  border-top: 1px dashed rgba(219, 255, 251, 0.2);
  border-bottom: 1px dashed rgba(219, 255, 251, 0.2);
}
/** 音频播放器*/
.aplayer {
  background-color: transparent;
  color: #fff;
  .aplayer-body {
    display: flex;
    flex-direction: column;
    .aplayer-pic {
    }
  }
}
/**弹框样式 */
.dialog-lineone {
  display: flex;
  margin: 0 0 20px 0;
  div:nth-child(1) {
    width: 50%;
  }
  div:nth-child(2) {
    width: 45%;
  }
  div:nth-child(3) {
    width: 5%;
  }
}
.change-audio {
  display: flex;
  margin-top: 20px;
  justify-content: center;
  div:nth-child(2) {
    margin-left: 20px;
  }
}
.scroll-hidden {
  height: 620px;
  overflow: auto;
}
.scroll-hidden::-webkit-scrollbar {
  display: none;
}

::v-deep {
  .el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    width: 195px;
  }

  .el-form-item__content {
    width: 150px;
  }
}
::v-deep {
  .el-dialog:not(.is-fullscreen) {
    margin-top: 30vh !important;
  }
}
::v-deep {
  .el-checkbox__input.is-disabled .el-checkbox__inner {
    background-color: rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.2);
  }
  .self {
    .el-table__body-wrapper {
      width: 100%;
      max-height: 260px;
      overflow: auto;
    }
  }
}
</style>
